var baseUrl = require('../../utils/urllink.js')

//获取应用实例
var app = getApp()
Page({
  data: {
    userInfo: {},
    store_id:0,
    user_other_id:0,
    list:[],
    page:1,
    page_size:10,
    store_avatar: '/imgs/no_head.jpg',
    store_banner: '',
    store_id: '',
    store_logo: '/imgs/no_logo.png',
    store_name: '',
    nicename:'', 
    head_pic1:'',
    guanzhu_show:false,
    agent_type:0,
    hostimgs: app.globalData.hostimgs,
  },

  onLoad: function (options) {
    var that = this;
    console.log('toshangping传过来的store_id参数' + options.store_id)
    var store_id = options.store_id;
    var user_other_id = options.user_id;
    that.setData({
      store_id:store_id,
      user_other_id:user_other_id,
      agent_type: app.globalData.agent_type,
    })

    //调用应用实例的方法获取全局数据
    // app.getUserInfo(function(userInfo){
    //   //更新数据
    //   that.setData({
    //     userInfo:userInfo
    //   })
    // })

    //获取集合
    getstorelist(that, user_other_id, store_id, 1, that.data.page_size)
    //判断是否关注店铺
    is_collect_store(that, app.globalData.user_id, store_id)
  },
  //跳转到注册绑定页面
  toregister: function (e) {
    wx.navigateTo({
      url: '../index/register',
    })
  },
  /**
* 页面相关事件处理函数--监听用户下拉动作
*/
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    // 获取集合
    getstorelist(that, that.data.user_other_id,that.data.store_id, that.data.page, that.data.page_size)
  },
  //封面图片
  photoshow: function () {

  },

  //预览图片
  previewImage: function (e) {
    var that = this;
    var current = e.target.dataset.src;
    var index = e.target.dataset.index;
    var images = [];
    for (var i = 0; i < that.data.list[index].gallery.length; i++) {
      images[i] = that.data.hostimgs + that.data.list[index].gallery[i].image_url
    }
    console.log(current);
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: images // 需要预览的图片http链接列表  
    })
  },

  //点赞 
  doc_like: function (event) {

    var that = this
    var goods_id = event.currentTarget.dataset.id;
    var is_proud = event.currentTarget.dataset.type;
    var index = event.currentTarget.dataset.index;
    console.log(is_proud)
    if (is_proud == 0) {
      //用户点赞
      goods_proud_op(that, app.globalData.user_id, goods_id, index)
      wx.showToast({
        title: '赞+1',
      })

    } else {
      //取消点赞
      goods_proud_op(that, app.globalData.user_id, goods_id, index)
      wx.showToast({
        title: '取消点赞',
      })
    }
    
  },

  //关注
  guanzhutap:function(e){
    var that = this

    // 收藏 和 取消收藏
    collect_store_op(that, app.globalData.user_id, that.data.store_id)
  },
  //跳转到商品详细页面
  todetail: function (e) {
    var that = this
    var goods_id = e.currentTarget.dataset.goodsid;
    wx.navigateTo({
      url: '../goods/detail?goods_id=' + goods_id,
    })
  },


})






//获取发布商品集合
function getstorelist(that,user_id,store_id, page, page_size) {
  console.log(user_id)
  console.log(store_id)
  //请求服务器
  wx.request({
    url: baseUrl+'weapp/Index/goods_list.html',
    data: {
      user_id:user_id,
      store_id:store_id,
      page: page,
      page_size: page_size,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: { 'content-type': 'application/json' }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      if (res.data.code == -1) {
        // 商品编号错误 没有返回数据
        wx.showModal({
          title: '提示',
          content: '店铺不存在',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.switchTab({
                url: '../mine/mine',
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else {
        if (that.data.page == 1) {
          that.setData({
            list: res.data.goods_list,
            head_pic1: res.data.store_logo,
            page: page + 1,
          })
        } else {
          var list = that.data.list;
          if(res.data.goods_list != null){
            for (var i = 0; i < res.data.goods_list.length; i++) {
              list.push(res.data.goods_list[i])
            }
            that.setData({
              list: list,
              page: page + 1,
            })
          }

        }
      }
      that.setData({
        store_banner: res.data.store_banner,
        store_id: res.data.store_id,
        store_name: res.data.store_name,
        
      })
      if(res.data.store_logo == '' || res.data.store_logo == null){

      }else{

        that.setData({
          store_logo: res.data.store_logo,
        })
      }
      if (res.data.store_avatar == '' || res.data.store_avatar == null) {

      }else{

        that.setData({
          store_avatar: res.data.store_avatar,
        })
      }
    },
    fail: function () { },
    complete: function () { }
  })
}

// 用户点赞 /取消点赞
function goods_proud_op(that, user_id, goods_id, index) {
  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Users/goods_proud_op.html',
    data: {
      user_id: user_id,
      goods_id: goods_id,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: { 'content-type': 'application/json' }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      if (res.data.code == 0) {
        //取消点赞
        var list = that.data.list;
        var prouds = list[index].proud;
        list[index].is_proud = 0;
        list[index].proud_count = list[index].proud_count - 1
        for (var i = 0; i < prouds.length; i++) {
          if (list[index].proud[i].user_id == user_id) {
            //删除这条记录
            list[index].proud.splice(i, 1)
          }
        }
        that.setData({
          list: list,
        })
      } else {
        var list = that.data.list;
        var prouds = list[index].proud;
        list[index].is_proud = 1;
        list[index].proud_count = list[index].proud_count + 1
        if (prouds.length <= 0) {
          list[index].proud[0] = res.data
        } else {
          list[index].proud.unshift(res.data)
        }
        that.setData({
          list: list,
        })
      }

    },
    fail: function () { },
    complete: function () { }
  })
}

//判断店铺关注接口
function is_collect_store(that, user_id, store_id) {
  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Users/is_collect_store.html',
    data: {
      user_id: user_id,
      store_id: store_id,

    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      if (res.data.code == 1) {
        // 关注了 code==1
        that.setData({
          guanzhu_show: true,
        })
      }
    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}

//店铺关注接口：Users / collect_store_op
function collect_store_op(that, user_id, store_id) {
  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Users/collect_store_op.html',
    data: {
      user_id: user_id,
      store_id: store_id,

    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      if (res.data.type == 'del') {
        wx.showToast({
          title: '取消关注',
        })
        that.setData({
          guanzhu_show: false,
        })
      } else {
        wx.showToast({
          title: '关注成功',
        })
        that.setData({
          guanzhu_show: true,
        })
      }
    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}